package com.gitee.denger.mybatis.example.ext.analytical;

import com.gitee.denger.mybatis.example.ext.util.SimpleReflectionUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

/**
 * andIn的注解处理
 *
 * @author 登琼
 */
public class ExampleCriteriaValueImpl4In implements IExampleCriteriaValue {

    @Override
    public void setForTk(Annotation annotation, Object criteria, String fieldName, Object fieldValue) {
        String methodName = lowerCaseForFirst(annotation.annotationType().getSimpleName());
        if (fieldValue instanceof Iterable) {
            Method method = SimpleReflectionUtils.findMethod(criteria.getClass(), methodName, String.class, Iterable.class);
            Iterable iterable = (Iterable) fieldValue;
            SimpleReflectionUtils.invokeMethod(method, criteria, fieldName, iterable);
        } else {
            throw new IllegalArgumentException(String.format("%s的值不是%s类型，出现错误", fieldName, Iterable.class));
        }
    }

    @Override
    public void set(Annotation annotation, Object criteria, String fieldName, Object fieldValue) {
        if (fieldValue instanceof List) {
            Method method = SimpleReflectionUtils.findMethod(criteria.getClass(), getMethodName(annotation, fieldName), List.class);
            List iterable = (List) fieldValue;
            SimpleReflectionUtils.invokeMethod(method, criteria, iterable);
        } else {
            throw new IllegalArgumentException(String.format("%s的值不是%s类型，出现错误", fieldName, List.class));
        }
    }
}